package com.china08.yxyapi.api.manage.edu.platform.statistics.resourcepandect1;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResResourceRepository;
import com.china08.yxyapi.repository.mg.lecture.YxyLecturePrePackRepository;
import com.china08.yxyapi.service.yxyresource.statistics.ResourceAllCount1Service;

/**
 * 
 * @Description 教育局平台办公 资源统计: 资源总量/共享资源数/区域资源数/资源使用数/备课包总数
 *
 * @Author LiuWeidong
 *
 * @Date 2017年12月7日
 *
 */
@CrossOrigin
@RestController
public class ResourceAllCount1Controller extends YxyApiBaseController {

    @Autowired
    public ResourceAllCount1Service resourceAllCountService1;

    @Autowired
    public YxyResResourceRepository yxyResResourceRepository;

    @Autowired
    YxyLecturePrePackRepository yxyLecturePrePackRepository;

    @Autowired
    JdbcTemplate JdbcTemplate;

    @RequestMapping(value = "/api_yw/manage/edu/platform/statistics/resourcepandect1/allcount/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response) {

        RespModel model = new RespModel();
        Integer schoolBureauLevel = getProfile(request, response).getSchoolBureauLevel();
        Long packCount = 0l;

        String districtCode = null;
        Integer resCount = 0;
        if (schoolBureauLevel == 0) {
            districtCode = getProfile(request, response).getSchoolAreaIdDistrict();
            // 资源使用数
            String resCountSql = " SELECT sum(pv) FROM yxy_res.yxy_res_resource  WHERE district_code = ? AND type_id != 'yx14'";
            resCount = JdbcTemplate.queryForObject(resCountSql, new Object[] { districtCode }, Integer.class);
            packCount = yxyLecturePrePackRepository.countByAreaIdDistrict(districtCode);

        } else if (schoolBureauLevel == 1) {
            districtCode = getProfile(request, response).getSchoolAreaIdCity();
            // 资源使用数
            String resCountSql = " SELECT sum(pv) FROM yxy_res.yxy_res_resource  WHERE city_code = ? AND type_id != 'yx14'";
            resCount = JdbcTemplate.queryForObject(resCountSql, new Object[] { districtCode }, Integer.class);
            packCount = yxyLecturePrePackRepository.countByAreaIdCity(districtCode);

        } else if (schoolBureauLevel == 2) {
            districtCode = getProfile(request, response).getSchoolAreaIdProvince();

            // 资源使用数
            String resCountSql = " SELECT sum(pv) FROM yxy_res.yxy_res_resource  WHERE province_code = ? AND type_id != 'yx14'";
            resCount = JdbcTemplate.queryForObject(resCountSql, new Object[] { districtCode }, Integer.class);

            packCount = yxyLecturePrePackRepository.countByAreaIdProvince(districtCode);
        }

        // 调用此方法查询资源数据总览数据
        List<Long> countLst = resourceAllCountService1.statisticsResourceCount(districtCode, schoolBureauLevel);

        ResourceAllCountModel resourceAllCountModel = new ResourceAllCountModel();
        // 资源总数量
        resourceAllCountModel.allCount = countLst.get(0);
        // 共享资源数
        resourceAllCountModel.shareCount = countLst.get(1);
        // 区域资源数
        resourceAllCountModel.areaCount = countLst.get(2);
        // 资源使用数
        resourceAllCountModel.resCount = Long.valueOf(resCount);
        // 备课包总数
        resourceAllCountModel.packCount = packCount;
        model.resourceAllCount = resourceAllCountModel;
        return model;
    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiPageReqModel {

    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {
        // 统计资源统计模型
        public ResourceAllCountModel resourceAllCount;
    }

    class ResourceAllCountModel {

        // 资源总数量
        public Long allCount;
        // 共享资源数
        public Long shareCount;
        // 区域资源数
        public Long areaCount;
        // 资源使用数
        public Long resCount;
        // 备课包总数
        public Long packCount;

    }
}
